/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.core.rtgen.entitygen.method;

import com.inspur.edp.bef.dtgenerator.core.rtgen.entitygen.entityimpl.BefEntityImplGenCtx;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.api.attr.ChildEntityAttribute;
import com.inspur.edp.cef.api.message.CefException;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.overall.GeneratorUtil;
import org.eclipse.jdt.core.dom.*;

import java.util.HashMap;
import java.util.Map;

public class CreateChildAccessorGenerator extends ClassMethodGenerator
{
	private BefEntityImplGenCtx entityImplGenCtx;
	private java.util.HashMap<String, ChildEntityAttribute> childEntitiesAttribute;
	public CreateChildAccessorGenerator(BefEntityImplGenCtx entityImplGenCtx, java.util.HashMap<String, ChildEntityAttribute> childEntitiesAttribute)
	{
		this.entityImplGenCtx = entityImplGenCtx;
		this.childEntitiesAttribute = childEntitiesAttribute;
	}

	private static final String paramChildCode = "childCode";
	private static final String paramData = "data";


	@Override
	protected java.util.ArrayList<Modifier.ModifierKeyword> getAccessModifiers()
	{
		return new java.util.ArrayList<Modifier.ModifierKeyword>(java.util.Arrays.asList(new Modifier.ModifierKeyword[] { Modifier.ModifierKeyword.PUBLIC_KEYWORD}));
	}

	@Override
	protected boolean getIsOverride() {
		return true;
	}

	@Override
	protected String getMethodName() {
		return "innerCreateChildAccessor";
	}

	@Override
	protected java.util.ArrayList<ParameterInfo> getParameterCollection()
	{
		ParameterInfo parameterInfo=new ParameterInfo();
		parameterInfo.setParamName(paramChildCode);
		parameterInfo.setParamType(new TypeInfo(String.class));
		ParameterInfo parameterInfo1=new ParameterInfo();
		parameterInfo1.setParamName(paramData);
		parameterInfo1.setParamType(new TypeInfo(IEntityData.class));
		return new java.util.ArrayList<ParameterInfo>(java.util.Arrays.asList(new ParameterInfo[] { parameterInfo,parameterInfo1}));
	}

	@Override
	protected TypeInfo getReturnType()
	{
		return new TypeInfo(IEntityData.class);
	}

	@Override
	protected Block buildMethodBody() {
		Block block=ast.newBlock();
		SwitchStatement switchStatement=ast.newSwitchStatement();
		MethodInvocation methodInvocation=ast.newMethodInvocation();
		methodInvocation.setExpression(ast.newSimpleName(paramChildCode));
		methodInvocation.setName(ast.newSimpleName("toLowerCase"));
		switchStatement.setExpression(methodInvocation);
		addSwitchCases(switchStatement);
		addDefaultCases(switchStatement);
		block.statements().add(switchStatement);
		return block;
	}

	private void addDefaultCases(SwitchStatement switchStatement) {
		SwitchCase switchCase=ast.newSwitchCase();
		switchCase.setExpression(null);
		switchStatement.statements().add(switchCase);
		addImport(CefException.class.getTypeName());
		ThrowStatement throwStatement=GeneratorUtil.getThrowStatement(ast,null);
//		ClassInstanceCreation classInstanceCreation=ast.newClassInstanceCreation();
//		classInstanceCreation.setType(new com.inspur.edp.caf.generator.baseInfo.TypeInfo(RuntimeException.class).getType(ast));
//		throwStatement.setExpression(classInstanceCreation);
		switchStatement.statements().add(throwStatement);
	}

//	private void addRootNodeCase(SwitchStatement switchStatement) {
//		SwitchCase switchCase=ast.newSwitchCase();
//		switchCase.setExpression(ExpressionUtils.getStringLiteral(ast,"RootNode"));
//		switchStatement.statements().add(switchCase);
//		switchStatement.statements().add(buildReturnAccessorStatement())
//	}

	private ReturnStatement buildReturnAccessorStatement(String accessorClassName,String dataClassName)
	{
		ReturnStatement returnStatement=ast.newReturnStatement();
		ClassInstanceCreation classInstanceCreation=ast.newClassInstanceCreation();
		classInstanceCreation.setType(ast.newSimpleType(ast.newName(accessorClassName)));

		CastExpression castExpression=ast.newCastExpression();
		castExpression.setType(ast.newSimpleType(ast.newName(dataClassName)));
		castExpression.setExpression(ast.newSimpleName(paramData));
		classInstanceCreation.arguments().add(castExpression);
		classInstanceCreation.arguments().add(ast.newBooleanLiteral(false));
		returnStatement.setExpression(classInstanceCreation);
		return returnStatement;
	}

	private void addSwitchCases(SwitchStatement switchStatement) {
		HashMap<String, MediateType> childTypes = entityImplGenCtx.getParentContext().getProjContext().getAccessorMediateTypes();
		if (childTypes == null || childTypes.size() == 0)
		{
			return;
		}
		if (childEntitiesAttribute == null)
		{
			return;
		}
for (Map.Entry<String, MediateType> item : childTypes.entrySet())
		{
			addCaseStatement(item,switchStatement);
		}
	}

	private void addCaseStatement(Map.Entry<String, MediateType> item, SwitchStatement switchStatement) {
		if (entityImplGenCtx.getParentContext().getProjContext().getEntityDataMediateTypes().containsKey(item.getKey()) == false)
		{
			return;
		}
		if (item.getValue().getClassName().endsWith("Acc") == false)
		{
			return;
		}

		SwitchCase switchCase=ast.newSwitchCase();
		switchCase.setExpression(ExpressionUtils.getStringLiteral(ast,item.getKey().toLowerCase()));
		switchStatement.statements().add(switchCase);
		switchStatement.statements().add(buildReturnAccessorStatement(item.getValue().getClassName(),entityImplGenCtx.getParentContext().getProjContext().getEntityDataMediateTypes().get(item.getKey()).getFullName()));
	}
}
